package com.lzw.breezeNovel.webApp.service.impl.comic;

import com.lzw.breezeNovel.common.exception.BaseException;
import com.lzw.breezeNovel.common.login.LoginUser;
import com.lzw.breezeNovel.common.login.LoginUserHolder;
import com.lzw.breezeNovel.common.result.ResultCodeEnum;
import com.lzw.breezeNovel.model.entity.pojo.comic.ComicHistory;
import com.lzw.breezeNovel.model.entity.pojo.user.UserInfo;
import com.lzw.breezeNovel.webApp.mapper.comic.ComicHistoryMapper;
import com.lzw.breezeNovel.webApp.mapper.user.UserInfoMapper;
import com.lzw.breezeNovel.webApp.service.comic.ComicHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 清风明月
 * @Package com.lzw.breezeNovel.webApp.service.impl.comic
 * @date 2025/3/18
 * @description 浏览记录
 */
@Slf4j
@Service
@SuppressWarnings("all")
public class ComicHistoryServiceImpl implements ComicHistoryService {

    /**
     * 注入mapper
     */
    @Autowired
    private ComicHistoryMapper comicHistoryMapper;
    /**
     * 注入用户mapper
     */
    @Autowired
    private UserInfoMapper userInfoMapper;


    /**
     * 保存浏览记录
     * @param comicId
     * @param orderNum
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Long comicId, Long orderNum) {
        //获取用户id
        Long userId = getUserId();
        ComicHistory comicHistory = comicHistoryMapper.selectByUserIdAndComicId(userId, comicId);
        if (comicHistory == null) {
            insertNewHistory(userId, comicId, orderNum);
        } else {
            updateExistingHistory(comicHistory, orderNum);
        }
    }

    /**
     * 插入新的浏览记录
     * @param userId 用户ID
     * @param comicId 漫画ID
     * @param orderNum 章节顺序
     */
    private void insertNewHistory(Long userId, Long comicId, Long orderNum) {
        ComicHistory comicHistory = new ComicHistory();
        comicHistory.setUserId(userId);
        comicHistory.setComicId(comicId);
        setCommonProperties(comicHistory, orderNum);
        comicHistoryMapper.insert(comicHistory);
    }

    /**
     * 更新现有的浏览记录
     * @param comicHistory 浏览记录
     * @param orderNum 章节顺序
     */
    private void updateExistingHistory(ComicHistory comicHistory, Long orderNum) {
        setCommonProperties(comicHistory, orderNum);
        comicHistoryMapper.update(comicHistory);
    }

    /**
     * 设置公共属性
     * @param comicHistory 浏览记录
     * @param orderNum 章节顺序
     */
    private void setCommonProperties(@NotNull ComicHistory comicHistory, Long orderNum) {
        comicHistory.setReadTime(new Date());
        comicHistory.setOrderNum(orderNum);
    }
    /**
     * 获取浏览记录
     * @param comicId
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public Long get(Long comicId) {
        //获取用户id
        Long userId = getUserId();
        //根据用户id和漫画id获取浏览记录
        ComicHistory comicHistory = comicHistoryMapper.selectByUserIdAndComicId(userId, comicId);
        if (comicHistory == null) {
            return 0L;
        }
        return comicHistory.getOrderNum();
    }

    /**
     * 获取用户信
     */
    public Long getUserId() {
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        //判断用户是否登录
        if (loginUser == null) {
            throw new BaseException(ResultCodeEnum.APP_LOGIN_AUTH);
        }
        String phone = loginUser.getPhone();
        //判断手机号查询用户信息
        UserInfo userInfo = userInfoMapper.selectByPhone(phone);
        if (userInfo == null) {
            throw new BaseException(ResultCodeEnum.APP_LOGIN_AUTH);
        }
        return userInfo.getUserId();
    }

}
